package com.ruge.test.网易云;

import com.netease.cloud.auth.BasicCredentials;
import com.netease.cloud.auth.Credentials;
import com.netease.cloud.services.nos.NosClient;
import com.netease.cloud.services.nos.model.CreateBucketRequest;
import com.netease.cloud.services.nos.model.ObjectMetadata;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.nio.channels.FileLockInterruptionException;

/**
 * 爱丽丝、如歌  创建于 2018/9/2 9:55
 * 说明:  TODO
 */
public class 对象存储_上传 {
	/**
	 * 本地文件普通上传
	 */
	@Test
	public void test1(){
		/**
		 * Access Key 4046a66045934bc6b366c0d3e995aceb
		 * Access Secret 1320ab11b4cb4c9c82b8404d0dce891e
		 */
		String accessKey = "4046a66045934bc6b366c0d3e995aceb";
		String secretKey = "1320ab11b4cb4c9c82b8404d0dce891e";
		Credentials credentials = new BasicCredentials(accessKey, secretKey);
		NosClient nosClient = new NosClient(credentials);
		String endPoint = "nos-eastchina1.126.net";
		nosClient.setEndpoint(endPoint);
		String filePath = "D:\\1.png";
		try {
			nosClient.putObject("ruge-security","ruge-security/headPhoto/demo2.png", new File(filePath));
		}catch (Exception e){
			System.out.println(e.getMessage());
		}
	}

	@Test
	public  void test2(){
		File file  = new File("D:\\1.png");
		String accessKey = "4046a66045934bc6b366c0d3e995aceb";
		String secretKey = "1320ab11b4cb4c9c82b8404d0dce891e";
		Credentials credentials = new BasicCredentials(accessKey, secretKey);
		NosClient nosClient = new NosClient(credentials);
		String endPoint = "nos-eastchina1.126.net";
		nosClient.setEndpoint(endPoint);
		try {
			ObjectMetadata objectMetadata = new ObjectMetadata();
			//设置流的长度，你还可以设置其他文件元数据信息
			nosClient.putObject("ruge-security", "ruge-security/headPhoto/demo3.png", new FileInputStream(file), objectMetadata);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}
